package com.css.fxfzaqbz.modules.estimate.repository.impl;

import com.css.fxfzaqbz.modules.estimate.entity.Fault;
import com.css.fxfzaqbz.modules.estimate.repository.FaultRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class FaultRepositoryImpl implements FaultRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void batchSave(List<Fault> faultList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(faultList.toArray());
        String sql = "INSERT INTO sys_fault (id,scale,fracture_zone_name,dc_name,fault_segment_name,strike_drection,strike,direction,clination,jf_length,top_depth,width,fracture_belt_width,feature,latest_active_period,start_time_est,vdisplace_est,vdisplace_er,hdisplace_est,hdisplace_er,tdisplace_est,tdisplace_er,ave_vrate,ave_vrate_er,ave_hrate,ave_hrate_rr,start_time_newest,new_vrate,new_vrateer,new_hrate,new_hrateer,max_vrate,max_vrate_er,max_hrate,max_hrate_rr,eq_event_count,eq_event_ribottom,eq_event_ritop,method,max_rupture_len,avg_rupture_len,elapse_time_latesteq,slip_depth_est,slip_depth_er,avg_slip_rateest,avg_slip_rateer,creep_rateest,creep_rateer,cs_max_slipest,cs_max_sliper,cs_ave_slipest,cs_ave_sliper,latest_cs_slipest,latest_cs_sliper,nsb1,nsb2,nsb3,show_code,active_fault,status,active_period,comment_info,source_flag,del_flag,data_version,create_user,create_time,update_user,update_time,geom,jf_code,province,city,county,town,extends1,extends2,extends3,extends4,extends5,extends6,extends7,extends8,extends9,extends10,feature_p,latest_active_period_p,result_analysis) values(:id,:scale,:fractureZoneName,:dcName,:faultSegmentName,:strikeDrection,:strike,:direction,:clination,:jfLength,:topDepth,:width,:fractureBeltWidth,:feature,:latestActivePeriod,:startTimeEst,:vdisplaceEst,:vdisplaceEr,:hdisplaceEst,:hdisplaceEr,:tdisplaceEst,:tdisplaceEr,:aveVrate,:aveVrateEr,:aveHrate,:aveHrateRr,:startTimeNewest,:newVrate,:newVrateer,:newHrate,:newHrateer,:maxVrate,:maxVrateEr,:maxHrate,:maxHrateRr,:eqEventCount,:eqEventRibottom,:eqEventRitop,:method,:maxRuptureLen,:avgRuptureLen,:elapseTimeLatesteq,:slipDepthEst,:slipDepthEr,:avgSlipRateest,:avgSlipRateer,:creepRateest,:creepRateer,:csMaxSlipest,:csMaxSliper,:csAveSlipest,:csAveSliper,:latestCsSlipest,:latestCsSliper,:nsb1,:nsb2,:nsb3,:showCode,:activeFault,:status,:activePeriod,:commentInfo,:sourceFlag,:delFlag,:dataVersion,:createUser,:createTime,:updateUser,:updateTime,ST_GeometryFromText(:geom,4490),:jfCode,:province,:city,:county,:town,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:featureP,:latestActivePeriodP,:resultAnalysis)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<Fault> getList(String resultAnalysis) {
        String sql = "select *,ST_AsText(geom) geom from sys_fault where result_analysis = '" + resultAnalysis + "'";
        List<Fault> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Fault.class));
        return list;
    }

    @Override
    public void delFault(String dataVersion) {
        String sql="delete from  sys_fault where data_version ='"+dataVersion+"'";
        jdbcTemplate.update(sql);
    }
}
